package com.ossjk.oa.advisory.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ossjk.core.base.controller.BaseController;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.Constant;
import com.ossjk.core.util.ExcelUtiles;
import com.ossjk.oa.advisory.entity.Customer;
import com.ossjk.oa.advisory.entity.Customerfw;
import com.ossjk.oa.advisory.entity.Discipline;
import com.ossjk.oa.advisory.entity.School;
import com.ossjk.oa.advisory.service.ICustomerService;
import com.ossjk.oa.advisory.service.ICustomerfwService;
import com.ossjk.oa.advisory.service.IDisciplineService;
import com.ossjk.oa.advisory.service.ISchoolService;
import com.ossjk.oa.advisory.vo.CustomerVo;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.entity.Token;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 学校对接人管理 前端控制器
 * </p>
 *
 * @author Jason
 * @since 2019-01-14
 */
@RestController
@RequestMapping("/advisory/customerrecycle")
public class CustomerrecycleController extends BaseController {
	@Autowired
	private ICustomerService iCustomerService;
	@Autowired
	private ICustomerfwService iCustomerfwService;
	@Autowired
	private ISchoolService iSchoolService;
	@Autowired
	private IDisciplineService iDisciplineService;

	@Autowired
	private IEmployeeService iEmployeeService;

	@RequiresPermissions("00150030")
	@GetMapping("/list")
	public Object list(String name, String sid, String did, String ectn, String period, String ottn, String elmt, String intt, String stime, String etime, Page page, HttpServletRequest request, HttpServletResponse response) {
		Wrapper wrapper = Condition.create().isWhere(true).eq("c.isdelete", 2);
		if (!CommonUtil.isBlank(name)) {
			wrapper.like("c.name", name);
		}
		if (!CommonUtil.isBlank(sid)) {
			wrapper.eq("c.sid", sid);
			if (!CommonUtil.isBlank(did)) {
				wrapper.eq("c.did", did);
			}
		}
		if (!CommonUtil.isBlank(ectn)) {
			wrapper.like("c.ectn", ectn);
		}
		if (!CommonUtil.isBlank(period)) {
			wrapper.like("c.period", period);
		}
		if (!CommonUtil.isBlank(ottn)) {
			wrapper.like("c.ottn", ottn);
		}
		if (!CommonUtil.isBlank(elmt)) {
			wrapper.like("c.elmt", elmt);
		}
		if (!CommonUtil.isBlank(intt)) {
			wrapper.like("c.intt", intt);
		}
		if (!CommonUtil.isBlank(stime)) {
			wrapper.ge("cfw2.mtime", stime);
		}
		if (!CommonUtil.isBlank(etime)) {
			wrapper.le("cfw2.mtime", etime);
		}
		wrapper.orderBy("cfw2.mtime", false).orderBy("crtm", false);
		Token token = (Token) request.getAttribute(Constant.REQUEST_TOKEN_KEY);

		String expression = token.getExpression();
		Map map = JSON.parseObject(expression, Map.class);
		List<String> object = (List<String>) map.get("psub");
		page = iCustomerService.selectVoPage(page, wrapper, getCurrnetUserId(), object);
		return responseSuccess(page);
	}

	@RequiresPermissions("001500300005")
	@DeleteMapping("/delete/{id}")
	public Object delete(@PathVariable("id") String[] id) {
		if (iCustomerService.deleteIds(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}

	@RequiresPermissions("001500300010")
	@PutMapping("/recovery/{id}")
	public Object recovery(@PathVariable("id") String[] id) {
		if (iCustomerService.batchUpdateRecovery(Arrays.asList(id))) {
			return responseSuccess();
		} else {
			return responseFail();
		}
	}
}
